﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Caliburn.Micro;
using SS.WPF.Framework.Selection;
using SS.WPF.Framework.ViewModel;
using SS.WPF.Framework.ViewModel.Container;
using SS.WPF.Framework.ViewModel.Navigation;

namespace TestFramework.ViewModels
{
    [Export(typeof(EditingViewModel))]
    public class EditingViewModel : OneActiveViewModel<IEditor>, ISelectionChangedHandler
    {
        [Import]
        public ISelectionService SelectionService
        {
            set
            {
                value.Register(this);
            }
        }

        private AddressBarViewModel _addressBarViewModel;
        public AddressBarViewModel AddressBarViewModel
        {
            get
            {
                return this._addressBarViewModel;
            }
            set
            {
                this._addressBarViewModel = value;
                this.NotifyOfPropertyChange(() => this.AddressBarViewModel);
            }
        }

        public EditingViewModel()
        {
            this.Show();
        }

        public void Show()
        {
            this.DisplayName = null;
            this.AddressBarViewModel = new AddressBarViewModel();
        }

        #region Overrides of Screen

        public override void ActivateItem(IEditor item)
        {
            base.ActivateItem(item);
            this.DisplayName = item != null ? item.DisplayName : null;
        }

        #endregion

        #region Implementation of IDisposable

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
        }

        #endregion

        #region Implementation of ISelectionChangedHandler

        public void SelectionChanged(object sender, ISelection newSelection)
        {
            if (newSelection != null && !newSelection.IsEmpty && newSelection is StructureSelection)
            {
                var structureSelection = (StructureSelection)newSelection;
                var editSupport = structureSelection.GetFirst() as IEditorDescription;
                if (editSupport != null)
                {
                    var instance = editSupport.Input;
                    if (instance == null)
                    {
                        var type = editSupport.EditorType;
                        instance = IoC.GetInstance(type, null);
                        editSupport.Input = instance;
                    }

                    var editor = instance as IEditor;
                    if (editor != null)
                    {
                        editor.Input = editSupport;
                    }
                    else
                    {
                        throw new Exception("Editor is not implemented IEditor interface");
                    }

                    var treeNode = editSupport as INavigationNode;
                    var pathItems = new List<object>();
                    if (treeNode != null)
                    {
                        var current = treeNode;
                        while (current != null)
                        {
                            pathItems.Add(current);
                            current = current.Parent;
                        }
                    }
                    else
                    {
                        pathItems.Add(editSupport.ToString());
                    }

                    pathItems.Reverse();
                    AddressBarViewModel.PathItems = pathItems.ToList();

                    this.ActivateItem(editor);
                }
                else
                {
                    this.ActivateItem(null);
                }
            }
            else
            {
                this.ActivateItem(null);
            }
        }

        #endregion
    }
}
